package org.kabanyo.components {

	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;
	import mx.styles.CSSStyleDeclaration;

	import org.kabanyo.components.supportClasses.ColorIndicator;
	import org.kabanyo.components.supportClasses.ToolTipRenderer;
	import org.kabanyo.skins.MapButtonSkin;

	import spark.components.supportClasses.ToggleButtonBase;
	import spark.primitives.Path;

	/**
	 * Button with map shape.
	 *
	 * @author Desky Natalio
	 *
	 */

	//Style

	/**
	 *  The map color.
	 *
	 *  @default 0x000000
	 */
	[Style(name="mapColor", type="uint", format="Color", inherit="no")]

	public class MapButton extends ToggleButtonBase {
		// Define a static variable.
		private static var classConstructed:Boolean=classConstruct();

		// Define a static method.
		private static function classConstruct():Boolean {
			if (!FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration("org.kabanyo.components.MapButton")) {
				// If there is no CSS definition for MapButton, then create one and set the default value.
				var mapStyles:CSSStyleDeclaration=new CSSStyleDeclaration();
				mapStyles.defaultFactory=function():void {
					this.mapColor=0x333333;
				}
				FlexGlobals.topLevelApplication.styleManager.setStyleDeclaration("org.kabanyo.components.MapButton", mapStyles, true);
			}
			return true;
		}

		//---------
		//Skin Part
		//---------
		[SkinPart(required="false")]
		public var pathDisplay:Path;

		private var _pathData:String;

		[Bindable]
		public function get pathData():String {
			return _pathData;
		}

		public function set pathData(value:String):void {
			_pathData=value;
		}

		private var _colorIndicators:Array;

		/**
		 * Set button color by indicated value.
		 * @return
		 *
		 */
		[ArrayElementType("org.kabanyo.components.IColorIndicator")]
		[Bindable]
		public function get colorIndicators():Array {
			return _colorIndicators;
		}

		/**
		 * @private
		 */
		public function set colorIndicators(value:Array):void {
			_colorIndicators=value;
		}

		private var _mapValue:Number;

		/**
		 * Indicated value.
		 */
		[Bindable]
		public function get mapValue():Number {
			return _mapValue;
		}

		/**
		 * @private
		 */
		public function set mapValue(value:Number):void {
			_mapValue=value;
			trace("Set Map Value");
			if (colorIndicators) {
				setStyle("mapColor", getColor());
			}
		}

		private var _toolTipData:Object;

		[Bindable]
		public function get toolTipData():Object {
			return _toolTipData;
		}

		public function set toolTipData(value:Object):void {
			_toolTipData=value;
			if (toolTipRenderer) {
				toolTipRenderer.data=value;
			}
		}

		private var _toolTipRenderer:IToolTipRenderer=new ToolTipRenderer();

		/**
		 *  The tooltip renderer to use for tooltipdata.
		 *
		 *  @default null
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public function get toolTipRenderer():IToolTipRenderer {
			return _toolTipRenderer;
		}

		/**
		 * @private
		 */
		public function set toolTipRenderer(value:IToolTipRenderer):void {
			_toolTipRenderer=value;
			if (value) {
				value.initiateToolTip(this);
			}
		}

		/**
		 * Constructor
		 *
		 */
		public function MapButton() {
			super();
			setStyle("skinClass", MapButtonSkin);
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}

		private function getColor():uint {
			colorIndicators.sortOn("minValue", Array.NUMERIC);
			colorIndicators.reverse();
			for each (var colorIndicator:ColorIndicator in colorIndicators) {
				if (mapValue <= colorIndicator.maxValue && mapValue >= colorIndicator.minValue) {
					trace("Choose map color from color indicators");
					return colorIndicator.color;
				}
			}
			return colorIndicator.color;
		}

		private function creationCompleteHandler(event:FlexEvent):void {
			if (colorIndicators) {
				setStyle("mapColor", getColor());
			}
			if (toolTipRenderer) {
				toolTipRenderer.data=toolTipData;
				toolTipRenderer.initiateToolTip(this);
			}
		}
	}
}
